<?php
/**
 * Created by Benjiemin
 * Date: 2020/3/6
 * Time: 19:33
 */
require ('./vendor/autoload.php');
require ('./aip-php-sdk-2.2.15/AipOcr.php');
use QL\QueryList;
use QL\Ext\Chrome;


$ql = QueryList::getInstance();
// 注册插件，默认注册的方法名为: chrome
$ql->use(Chrome::class);
$ql->chrome(function ($page,$browser) {
    $base = 'https://www.ss525.com/';
    $page->goto($base.'home/');
    // 这里故意设置一个很长的延长时间，让你可以看到chrome浏览器的启动
    sleep(3);
    $page->type("input[id='userName']",'jianxiansheng');
    $page->type("input[id='userPwd']",'xiaxia123');
    $filename = 'ss525ValidateCode.jpeg';
    $imgUrl = $base.'api/getValidateCode.do?_=0.'.time();
    $page->goto($imgUrl);
    file_put_contents($filename,$page->content());

    //验证码识别
    $appId = '9360309';
    $apiKey = 'f94FyaKX2LKx0GWBoddFVpxa';
    $secretKey = 'oGhM9QqHH6qslErF1LH8etP6Ber5adDz';
    $apiOcrModel = new AipOcr($appId, $apiKey, $secretKey);
    $verifyRet = $apiOcrModel->basicGeneral(file_get_contents($filename));

    if (!isset($verifyRet['words_result'][5]['words'])) {
        echo '识别验证码失败';
        die;
    }
    $validateCode = $verifyRet['words_result'][5]['words'];
    $page->type("input[id='loginVcode']",$validateCode);
    sleep(1);
    //点击登录
    $page->click("input[type='submit']");

    //等待搜索结果
    sleep(3);
    $html = '';
    //如果有需要，可以把$rt入库，以及做其他操作
    sleep(6000);
    $browser->close();
    // 返回值一定要是页面的HTML内容
    return $html;
},[
    'headless' => false, // 启动可视化Chrome浏览器,方便调试
    'devtools' => false, // 打开浏览器的开发者工具
])->find('title')->text();
